//
//  Timer.swift
//  socksTest
//
//  Created by liujianlong on 2023/6/27.
//

import UIKit

class MMTimer {
    fileprivate var timer: DispatchSourceTimer?
    private(set) var isRunning = false
    
    func startTimer(interval: DispatchTimeInterval, queue: DispatchQueue = DispatchQueue.main, handler:@escaping () -> Void) {
        if timer == nil {
            timer = DispatchSource.makeTimerSource(queue: queue)
            timer?.setEventHandler(handler: handler)
            timer?.schedule(deadline: .now() + interval, repeating: interval)
            timer?.resume()
            isRunning = true
        }
        if !isRunning {
            timer?.resume()
            isRunning = true
        }
    }
    
    func stopTimer() {
        if isRunning {
            timer?.cancel()
            timer = nil
            isRunning = false
        }
    }
}
